﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

using TRPOP.Config;
using TRPOP.Common;
using TRPOP.CMS.Plugin;
using Newtonsoft.Json;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.UI
{
    public class SwfUpload_Upload : BaseAdmin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["jsNode"] == null)
            {
                Alert("请通过编辑器上传图片");
                Response.End();
                return;
            }

            string strBaseUrl = "/resource";
            JavaScriptObject jsNode = (JavaScriptObject)Session["jsNode"];
            if (jsNode.ContainsKey("savepath"))
            {
                strBaseUrl = jsNode["savepath"].ToString();
                strBaseUrl = strBaseUrl + "/";
                strBaseUrl = strBaseUrl.Replace("//", "/");
            }

            // 新的上传图片的方法
            string strFilePath = strBaseUrl.Replace("~/", "/");
            string strUploadFileName = string.Empty;
            string strUploadFileWithPath = string.Empty;
            AttachmentInfo oAttachmentInfo = TRPOP.Common.EditorImgHelper.EditorUploadFile(ref strUploadFileName, ref strUploadFileWithPath, strFilePath, SYSConfig.Attachment_UpImageMaxWidth);
            oAttachmentInfo.FileName = strUploadFileWithPath;
            //

            if (oAttachmentInfo != null && oAttachmentInfo.ErrorDescription == "")
            {
                List<AttachmentInfo> olist = (List<AttachmentInfo>)Session["admin_uploadfiles"];
                if (olist == null)
                    olist = new List<AttachmentInfo>();
                Session["admin_uploadfiles"] = olist;
                olist.Add(oAttachmentInfo);
                
                Response.Clear();
                Response.StatusCode = 200;
                Response.Write(olist.Count);
                Response.End();
            }
            else
            {
                Response.Clear();
                Response.StatusCode = 500;
                Response.Write("An error occured");
                Response.End();
            }
        }
    }
}
